<?php


namespace app\admin\model;


use app\common\controller\AdminController;
use app\common\lib\RespCode;
use custom\Err;
use custom\Redis;
use think\Db;

class HouseStatistics extends AdminController {

    /**
     * 统计列表
     * @param $params
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($params) {
        $count = db('house_statistics_city_month')
            ->where('city_code', $params['city_code'])
            ->count();
        if ($count == 0) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
            return $result;
        } else {
            $data = db('house_statistics_city_month')
                ->where('city_code', $params['city_code'])
                ->page($params['page'], $params['long'])
                ->order('id desc')
                ->field('count_time,deal_total_number,deal_rate,deal_money,bidders_number,id,info_url')
                ->select();
            $pageNum = ceil($count / $params['long']);
            foreach ($data as $k => $v) {
                $data[$k]['deal_money'] = $v['deal_money'] / 1000000;
                $data[$k]['title'] = date('m', $v['count_time']) . '月成交情报';
                $data[$k]['count_time'] = date('Y-m', $v['count_time'] + 60 * 60 * 24 * 32) . '-01 00:00:00';
            }
            $result = [
                'page'    => (int)$params['page'],
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
            return $result;
        }
    }

    /**
     * 房源预览
     * @param $params
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function preview($params) {
        date_default_timezone_set('PRC');
        $data = db('house_statistics_city_month')
            ->find($params['id']);
        if ($data['deal_total_number'] == 0) {
            Err::add('成交房源为0', RespCode::QUERY_ERR);
            return false;
        }
        $city_code = $data['city_code'];
        $city_codes = Db::name('region')
            ->where('p_code', $city_code)
            ->order('weights asc')
            ->column('city_code');
        $map[] = ['h.city_code', 'in', $city_codes];
        $start_time = $data['count_time'];
        $end_time = strtotime(date('Y-m', $data['count_time'] + 60 * 60 * 24 * 31) . '-01');
        $map[] = ['ha.deal_time', '>=', $start_time];
        $map[] = ['ha.deal_time', '<', $end_time];
        $map[] = ['ha.auction_status', '=', 3];
        $city_codes_str = join(',', $city_codes);
        $list = House::previewHouse($map, $city_codes_str);
        $redisModel = Redis::getInstance();
        $time = time();
        $title = date('m', $data['count_time']) . '月成交情报';
        $redisModel->hset("house_statistics_Preview_resp", $time, json_encode(['list' => $list]));
        $redisModel->hset("house_statistics_Preview_request", $time, json_encode(['start_time' => $start_time, 'end_time' => $end_time, 'city' => Region::where('city_code', $city_code)->value('name'), 'id' => $params['id'], 'title' => $title]));
        return $time;
    }

    public static function encodeRespKey($params) {
        $redisModel = Redis::getInstance();
        $data = $redisModel->hget("house_statistics_Preview_resp", $params['key']);
        $data = json_decode($data, true);
        return $data;
    }

    public static function encodeRequestKey($params) {
        $redisModel = Redis::getInstance();
        $data = $redisModel->hget("house_statistics_Preview_request", $params['key']);
        $data = json_decode($data, true);
        return $data;
    }


    /**
     * 修改
     * @param $params
     * @param $id
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function post($params, $id) {
        if (!db('house_statistics_city_month')
            ->where('id', $id)
            ->update(['info_url' => $params['img']])) {
            Err::add('发布失败', RespCode::DATA_ERR);
            return false;
        } else {
            return true;
        }
    }

}